package com.shyroke.daydayzhuanweb.controller;

import com.google.gson.Gson;
import com.querydsl.jpa.impl.JPAQueryFactory;
import com.shyroke.daydayzhuanapi.dto.ProjectDto;
import com.shyroke.daydayzhuanapi.entity.BusinessEntity;
import com.shyroke.daydayzhuanapi.entity.ProjectEntity;
import com.shyroke.daydayzhuanapi.entity.QBusinessEntity;
import com.shyroke.daydayzhuanapi.entity.TypeEntity;
import com.shyroke.daydayzhuandao.respository.TypeRepository;
import com.shyroke.daydayzhuansvc.service.IOSSService;
import com.shyroke.daydayzhuansvc.service.IProjectService;
import com.shyroke.daydayzhuansvc.util.Page;
import com.shyroke.daydayzhuansvc.util.TabelData;
import com.shyroke.daydayzhuansvc.util.UserUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.util.CollectionUtils;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletResponse;
import java.util.List;

/**
 * 类名 ：导航栏控制层
 * 用法 ：
 * 创建人 ： shyroke
 * 时间：2018/12/4 9:44
 */
@Controller
@RequestMapping(value = "menu")
public class MenuController {
    @Autowired
    private TypeRepository typeRepository;
    @Autowired
    private IProjectService projectService;
    @Autowired
    private IOSSService ossServicec;
    @Autowired
    private JPAQueryFactory factory;

    private Logger logger = LoggerFactory.getLogger(this.getClass());

    /**
     * 跳转到普通项目（首页、网赚资讯）首页
     * @return
     */
    @GetMapping(value = "project/{id}")
    public ModelAndView project(ModelAndView model, @PathVariable("id")Integer typeId, Page page, HttpServletResponse response){

        Gson gson = new Gson();

        //如果是首页则跳转到首页
        if(0==typeId){
            response.setStatus(301);
            response.setHeader("Location", "https://www.daydayzhuan.com");
            return null;
        }

        model = UserUtils.getMenuList(model,typeId);

        //1、判断该节点是父菜单还是子菜单，如果是子菜单则返回父菜单，用于首页选中菜单常亮显示。
        TypeEntity type = typeRepository.findById(typeId).get();
        if(type.getParentId() == 0){
            model.addObject("typeId",typeId);
        }else{
            model.addObject("typeId",type.getParentId());
        }

         //2、 获取轮播图的广告项目
//        List<ProjectEntity> commentProjectList = projectService.getCommentProjectList();
//        for(ProjectEntity project:commentProjectList){
//            if(!StringUtils.isEmpty(project.getCover())){
//                project.setCover(ossServicec.getImgUrl(project.getCover()));
//            }
//        }

        //获取首页分页信息
        TabelData<ProjectDto> projectList =  projectService.getProjectByTypeIdPage(page,typeId,false);

//        model.addObject("commentProjectList",commentProjectList);
        model.addObject("rel_typeId",typeId);

        model.addObject("projectList", projectList.getData());
        page.setCount(projectList.getCount());
        model.addObject("page", gson.toJson(page));


        model.setViewName("article/list");
            return model;
    }

    /**
     * 跳转到商务合作的页面
     * @return
     */
    @RequestMapping(value = "bussiness")
    public ModelAndView bussiness(ModelAndView model){
        model = UserUtils.getMenuList(model,null);
        QBusinessEntity qBusinessEntity = QBusinessEntity.businessEntity;
        List<BusinessEntity> list = factory.selectFrom(qBusinessEntity).limit(1).fetch();
        model.setViewName("bussiness");
        model.addObject("business", CollectionUtils.isEmpty(list)?null:list.get(0));
        return model;
    }

    /**
     * 跳转到薅羊毛首页
     * @return
     */
    @GetMapping(value = "yangmao/{id}")
    public ModelAndView yangmao(HttpServletResponse response){
        response.setStatus(301);
        response.setHeader("Location", "https://www.daydayzhuan.com/yangmao");
        return null;
    }

    /**
     * 跳转到长期赚钱首页
     * @return
     */
    @GetMapping(value = "longtime/{id}")
    public ModelAndView longtime(HttpServletResponse response){
        response.setStatus(301);
        response.setHeader("Location", "https://www.daydayzhuan.com/longtime");
        return null;
    }

    /**
     * 跳转到网赚闲谈首页
     * @return
     */
    @GetMapping(value = "news/{id}")
    public ModelAndView news(HttpServletResponse response){
        response.setStatus(301);
        response.setHeader("Location", "https://www.daydayzhuan.com/news");
        return null;
    }

    public static void main(String[] args) {
        f1(94,35);
        f2(94,35);
    }

   static void f1(int footNum , int headNum) {
        for (int i = 0; i < headNum; i++) {
            int j = headNum-i;
            if (i * 2 + j * 4 == footNum) {
                System.out.println("兔子" + j + "个");
                System.out.println("鸡" + i + "只");
                break;
            }
        }
    }

    static void f2(int footNum, int headNum) {
        Integer chicken = null;
        Integer rabbit = null;


    }

    /**
     * 跳转到区块链首页
     * @return
     */
    @GetMapping(value = "blockChain/{id}")
    public ModelAndView blockChain(HttpServletResponse response){
        response.setStatus(301);
        response.setHeader("Location", "https://www.daydayzhuan.com/news");
        return null;
    }



}
